package com.dms.controller;

import com.dms.pojo.Class;
import com.dms.pojo.College;
import com.dms.pojo.Major;
import com.dms.pojo.Student;
import com.dms.service.CollegeManageService;
import com.dms.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @className: CollegeManageController
 * @package: com.dms.controller
 * @description  院部管理模块控制层
 * @author: sk
 */
@Controller
@RequestMapping("/collegeManageController")
public class CollegeManageController {

    @Autowired
    CollegeManageService collegeManageService;

    /**
     *  获取院部列表集合 模糊分页查询
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getCollegeListLimit")
    @ResponseBody
    public Msg getCollegeListLimit(@RequestParam(value = "query") String query, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
        return Msg.success().add("collegeList", collegeManageService.getCollegeListLimit(query, pageNum, pageSize)).add("total", collegeManageService.getCollegeTotal(query));
    }

    /**
     *  检查院部名是否已经存在
     * @param collegeName
     * @return
     */
    @GetMapping("/checkCollegeName")
    @ResponseBody
    public Msg checkCollegeName(@RequestParam("collegeName") String collegeName){
        return collegeManageService.checkCollegeName(collegeName) ? Msg.success() : Msg.fail();
    }

    /**
     *  添加院部
     * @param college
     * @return
     */
    @PostMapping("/addCollege")
    @ResponseBody
    public Msg addCollege(@RequestBody College college){
        return collegeManageService.addCollege(college) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     *  获取院部信息
     * @param collegeName
     * @return
     */
    @GetMapping("/getCollegeInfo")
    @ResponseBody
    public Msg getCollegeInfo(@RequestParam("collegeName") String collegeName){
        College college = collegeManageService.getCollegeInfo(collegeName);
        if (college != null)
            return Msg.success().add("collegeInfo",college);
        return Msg.fail();
    }

    /**
     * 修改院部信息
     * @param college
     * @return
     */
    @PutMapping("/editCollege")
    @ResponseBody
    public Msg editCollege(@RequestBody College college){
        return collegeManageService.editCollege(college) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     * 删除院部信息
     * @param collegeName
     * @return
     */
    @DeleteMapping("/deleteCollege")
    @ResponseBody
    public Msg deleteCollege(@RequestParam("collegeName") String collegeName){
        return  collegeManageService.deleteCollege(collegeName) > 0 ? Msg.success() : Msg.fail();
    }

    /*************************************************************************************************/

    /**
     * 模糊查询  获取专业列表集合
     * @param select
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getMajorListLimit")
    @ResponseBody
    public Msg getMajorListLimit(@RequestParam("select") String select, @RequestParam(value = "query") String query, @RequestParam("pageNum") int pageNum,
                                 @RequestParam("pageSize") int pageSize){
        return Msg.success().add("majorList", collegeManageService.getMajorListLimit(select, query, pageNum, pageSize))
                .add("collegeList", collegeManageService.getCollegeList())
                .add("total", collegeManageService.getMajorTotal(select, query));
    }

    /**
     * 获取学院专业集合
     * @return
     */
    @GetMapping("/getMajorList")
    @ResponseBody
    public Msg getMajorList(){
        return Msg.success().add("majorList",collegeManageService.getCollegeList());
    }

    /**
     *  专业名查重
     * @param majorName
     * @return
     */
    @GetMapping("/checkMajorIsExist")
    @ResponseBody
    public Msg checkMajorIsExist(@RequestParam("majorName")String majorName){

        return collegeManageService.checkMajorIsExist(majorName) ? Msg.success() : Msg.fail();
    }

    /**
     * 添加专业信息
     * @param major
     * @return
     */
    @PostMapping("/addMajor")
    @ResponseBody
    public Msg addMajor(@RequestBody Major major){
        return collegeManageService.addMajor(major) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     * 获取major信息
     * @param majorName
     * @return
     */
    @GetMapping("/getMajorInfo")
    @ResponseBody
    public Msg getMajorInfo(@RequestParam("majorName")String majorName){
        return Msg.success().add("majorInfo", collegeManageService.getMajorInfo(majorName));
    }

    /**
     * 修改专业信息
     * @param major
     * @return
     */
    @PutMapping("/editMajor")
    @ResponseBody
    public Msg editMajor(@RequestBody Major major){
      return collegeManageService.editMajor(major) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     * 删除专业信息
     * @param majorName
     * @return
     */
    @DeleteMapping("/deleteMajor")
    @ResponseBody
    public Msg deleteMajor(@RequestParam("majorName")String majorName){
        return collegeManageService.deleteMajor(majorName) > 0 ? Msg.success() : Msg.fail();
    }

    /***************************************************************************************/

    /**
     * 获取学院专业级联列表
     * @return   http://127.0.0.1:9999/collegeManageController/getCollegeList
     */
    @GetMapping("/getCollegeList")
    @ResponseBody
    public Msg getCollegeList(){
        return Msg.success().add("collegeList", collegeManageService.getCollegeList());
    }

    /**
     * 获取班级列表
     * @param select
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getClassListLimit")
    @ResponseBody
    public Msg getClassListLimit(@RequestParam(value = "select",required = false) Integer select, @RequestParam(value = "query") String query, @RequestParam("pageNum") int pageNum,
                                 @RequestParam("pageSize") int pageSize) {
        return Msg.success().add("classList", collegeManageService.getClassListLimit(select, query, pageNum, pageSize))
                .add("total", collegeManageService.getClassTotal(select, query));
    }

    /**
     * 班级查重
     * @param majorId
     * @param className
     * @return
     */
    @GetMapping("/checkClass")
    @ResponseBody
    public Msg checkClass(@RequestParam("majorId") int majorId, @RequestParam("className") String className){
        return collegeManageService.checkClass(majorId, className) ? Msg.success() : Msg.fail();

    }

    /**
     * 添加班级
     * @return
     */
    @PostMapping("/addClass")
    @ResponseBody
    public Msg addClass(@RequestBody Class aClass){
        return collegeManageService.addClass(aClass) > 0 ? Msg.success() : Msg.fail();
    }

    /**
     * 获取班级信息
     * @param majorId
     * @param className
     * @return
     */
    @GetMapping("/getClassInfo")
    @ResponseBody
    public Msg getClassInfo(@RequestParam("majorId") int majorId, @RequestParam("className") String className){
        return Msg.success().add("classInfo", collegeManageService.getClassInfo(majorId, className));
    }


    /**
     * 修改班级信息
     * @return
     */
    @PutMapping("/editClass")
    @ResponseBody
    public Msg editClass(@RequestBody Class aClass){
        int num = collegeManageService.editClass(aClass);
        //  >0:可以修改  =0:修改失败  <0:该修改数据已存在，不可修改
        return num > 0 ? Msg.success() : num == 0 ? Msg.fail() : Msg.exception();
    }

    /**
     * 删除班级信息
     * @param className
     * @return
     */
    @DeleteMapping("/deleteClass")
    @ResponseBody
    public Msg deleteClass(@RequestParam("className") String className){
        return collegeManageService.deleteClass(className) > 0 ? Msg.success() : Msg.fail();
    }

    /***********************************************************************************************/

    /**
     *  模糊查询获取学生信息
     * @param majorId  专业名称
     * @param buildingId 楼栋号
     * @param stuNumber 学号
     * @param stuName 姓名
     * @param classId 班级名称
     * @return
     */
    @GetMapping("/getStudentListLimit")
    @ResponseBody
    public Msg getStudentListLimit(@RequestParam(value = "majorId",required = false) Integer majorId, @RequestParam(value = "buildingId",required = false) Integer buildingId,
                                   @RequestParam(value = "stuNumber",required = false) String stuNumber, @RequestParam(value = "stuName",required = false) String stuName,
                                   @RequestParam(value = "className",required = false) Integer classId, @RequestParam("pageNum") int pageNum,
                                   @RequestParam("pageSize") int pageSize){
        return Msg.success().add("studentList", collegeManageService.getStudentListLimit(majorId, buildingId, stuNumber, stuName, classId, pageNum, pageSize))
                .add("total", collegeManageService.getStudentTotal(majorId, buildingId, stuNumber, stuName, classId));
    }

    /**
     * 学号查重
     * @param userName
     * @return
     */
    @GetMapping("/checkStudent")
    @ResponseBody
    public Msg checkStudent(@RequestParam("userName") String userName){
        return collegeManageService.getStudentByUserName(userName) == null ? Msg.success() : Msg.fail();
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/addStudent")
    @ResponseBody
    public Msg addStudent(@RequestBody Student student){
        int count = collegeManageService.addStudent(student);
        return  count == 3 || count == 2? Msg.success() : Msg.fail();
    }

    /**
     * 编辑学生
     * @param student
     * @return
     */
    @PutMapping("/editStudent")
    @ResponseBody
    public Msg editStudent(@RequestBody Student student){
        int num = collegeManageService.editStudent(student);
        return num == 3 || num == 1 ? Msg.success() : num == 210 ? Msg.exception() : Msg.fail();
    }

    /**
     * 删除学生
     * @param userName
     * @param dormitoryId
     * @param stuName
     * @return
     */
    @DeleteMapping("/deleteStudent")
    @ResponseBody
    public Msg deleteStudent(@RequestParam("userName") String userName, @RequestParam(value = "dormitoryId",required = false) Integer dormitoryId, @RequestParam("stuName") String stuName){
        int count = collegeManageService.deleteStudent(userName, dormitoryId, stuName);
        return  count == 2 || count == 1 ? Msg.success() : Msg.fail();
    }

}
